/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.core.fcomps.delegate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jbfilter.core.CleaningStrategy;
import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.FilterComponent;
/**
 * Delegates 'write' methods to every filter component, reads the 'read' methods from
 * one filter component.
 * @author Marcus Adrian
 * @param <E> the type of the objects the filter component is conceived for
 */
public class FilterComponentDelegate<E>
	implements
		FilterComponent<E> {

	private List<FilterComponent<E>> fComps = new ArrayList<FilterComponent<E>>();
	
	public void add(FilterComponent<E> filterComponent) {
		fComps.add(filterComponent);
	}
	public void addAll(Collection<FilterComponent<E>> filterComponents) {
		fComps.addAll(filterComponents);
	}
	public List<FilterComponent<E>> getFilterComponents() {
		return fComps;
	}
	private FilterComponent<E> getFirst() {
		for (FilterComponent<E> fComp : fComps) {
			return fComp;
		}
		throw new IllegalStateException("No filter component set.");
	}
	@Override
	public void setEnabled(boolean enabled) {
		for (FilterComponent<E> fComp : fComps) {
			fComp.setEnabled(enabled);
		}
	}
	@Override
	public boolean isEnabled() {
		return getFirst().isEnabled();
	}
	@Override
	public boolean isActive() {
		return getFirst().isActive();
	}
	@Override
	public void clean() {
		for (FilterComponent<E> fComp : fComps) {
			fComp.clean();
		}
	}
	@Override
	public void setCleaningStrategy(CleaningStrategy cleaningStrategy) {
		for (FilterComponent<E> fComp : fComps) {
			fComp.setCleaningStrategy(cleaningStrategy);
		}
	}
	@Override
	public String getId() {
		return getFirst().getId();
	}
	@Override
	public void setId(String id) {
		for (FilterComponent<E> fComp : fComps) {
			fComp.setId(id);
		}
	}
	@Override
	public boolean isInverted() {
		return getFirst().isInverted();
	}
	@Override
	public void setInverted(boolean inverted) {
		for (FilterComponent<E> fComp : fComps) {
			fComp.setInverted(inverted);
		}
	}
	@Override
	public void setEnableStrategy(EnableStrategy enableStrategy) {
		for (FilterComponent<E> fComp : fComps) {
			fComp.setEnableStrategy(enableStrategy);
		}
	}
	@Override
	public CleaningStrategy getCleaningStrategy() {
		return getFirst().getCleaningStrategy();
	}
	@Override
	public EnableStrategy getEnableStrategy() {
		return getFirst().getEnableStrategy();
	}
}
